﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;


namespace SmartDeviceProject2
{
    class Program
    {
        static void Main(string[] args)
        {
            TimerExampleState s = new TimerExampleState();
            TimerCallback timerDelegate = new TimerCallback(CheckStatus);
            Timer timer = new Timer(timerDelegate, s, 1000, 1000);
            s.tmr = timer;

            //  while(s.tmr != null)
            //Thread.Sleep(0);
            while (true)
            {
                if (s.tmr == null)
                    break;
            }
            Console.WriteLine("Timer example done.");
            Console.ReadLine();
            
        }
        public static void CheckStatus(Object state)
        {
            TimerExampleState s = (TimerExampleState)state;
            s.counter++;
            Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter);
            if (s.counter == 5)
            {
                (s.tmr).Change(1000, 2000);//更改计时器的启动时间和方法调用之间的间隔
                Console.WriteLine("changed...");
            }
            if (s.counter == 10)
            {
                Console.WriteLine("disposing of timer...");
                s.tmr.Dispose();
                s.tmr = null;
            }
        }
    }

    class TimerExampleState
    {
        public int counter = 0;
        public Timer tmr;
    }

}
